package com.nbjtjc.safe.api.sys;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.nbjtjc.safe.api.AbstractController;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysPermissionMapper;
import com.nbjtjc.safe.model.sys.SysPermission;
import com.nbjtjc.safe.service.sys.SysPermissionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统权限
 */
@RestController
@RequestMapping("/permission")
@CrossOrigin(allowCredentials = "true",origins = {"*"})
public class SysPermissionController extends AbstractController<SysPermission> {
    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysPermissionService permissionService;

    @RequestMapping(value = "/add",method = {RequestMethod.POST})
//    @RequiresPermissions("sys:per:add")
    public Result<SysPermission> add(SysPermission sysPermission){
        Result<SysPermission> result=new Result<>();
        try {
            if(sysPermission==null){
                result.setFalse("不能为空");
                return result;
            }
            sysPermissionMapper.insert(sysPermission);
            result.setData(sysPermission);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }

    @Override
    public Result delete(int id) {
        return null;
    }

    @RequestMapping(value = "/find",method = {RequestMethod.GET})
    @Override
    public Result find(SysPermission param, Page page) {
        Result<PageInfo> result=new Result<>();
        try {
            PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
//            List<SysPermission> permissions = permissionService.findMenuList(getUserId());
            List<SysPermission> permissions = permissionService.getUserPermission(getUserId());
            result.setData(new PageInfo(permissions));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/menu",method = {RequestMethod.POST})
    public Result<List<SysPermission>> menu(){
        Result<List<SysPermission>> result=new Result<>();
        try {
//            List<SysPermission> treeNodes=permissionService.findMenuList(getUserId());
            List<SysPermission> treeNodes=permissionService.getUserPermission(getUserId());
            result.setData(treeNodes);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/findAllMenu",method = {RequestMethod.POST})
    public Result<List<SysPermission>> findAllMenu(){
        Result<List<SysPermission>> result=new Result<>();
        try {
            List<SysPermission> treeNodes=permissionService.getUserPermission(Constant.SUPER_ADMIN);
            result.setData(treeNodes);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/update",method = {RequestMethod.POST})
//    @RequiresPermissions("sys:per:update")
    public Result<SysPermission> update(SysPermission permission){
        Result<SysPermission> result=new Result<>();
        try {
            if(ObjectUtil.isNull(permission)||ObjectUtil.isNull(permission.getId())){
                result.setFalse("不能为空");
                return result;
            }
            int count=sysPermissionMapper.updateByPrimaryKeySelective(permission);
            System.out.println(count);
            result.setData(permission);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
//    @RequiresPermissions("sys:per:delete")
    public Result<Boolean> delete(SysPermission sysPermission){
        Result<Boolean> result=new Result<>();
        try {
            int num=sysPermissionMapper.delete(sysPermission);
            System.out.println(num);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("错误信息："+e.getMessage());
        }
        return result;
    }


}
